﻿using CommonFactory;
using SkillSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Reflection;
using EnvironSystem;

namespace CharacterSystem
{
    public abstract class CharacterBase : MonoBehaviour,ICharacter,IEnvironmentElement
    {
        public Animator CharacterAnimator { get { return characterAnimator; } }
        [SerializeField]
        private Animator characterAnimator;

        public Transform Trans { get { return this.transform; } }

        public Dictionary<int,ActiveSkillTransmitter> ActiveSkillTransmitters=new Dictionary<int, ActiveSkillTransmitter>();

        public Dictionary<int, SkillReceiverBase> SkillReceivers = new Dictionary<int, SkillReceiverBase>();

        public CharacterController CharacterCtrl;
        protected INavicater navicate;
        public ICharacterData CurCharacterData;

        public float SpeedScale {
            set
            {
                speedScale = value;
                CharacterAnimator.speed = SpeedScale;
            }
            get { return speedScale; }
        }

        public Vector3 Position
        {
            get
            {
                return transform.position;
            }
        }

        protected float speedScale=1;

        public float MoveSpeed=2;


        public void Init(ICharacterData data) {
            CurCharacterData = data;
        }

        public T AddComponent<T>() where T : Component
        {
            return this.gameObject.AddComponent<T>();
        }

        public void MoveTo(Vector3 targetPos)
        {
            if (navicate!=null)
            {
                navicate.SetDestination(targetPos);
            }
            else
            {
                Vector3 speed = targetPos - transform.position;
                CharacterCtrl.SimpleMove(speed.normalized* MoveSpeed*SpeedScale);
            }
            targetPos.y = this.transform.position.y;
            transform.LookAt(targetPos);
        }

        private void OnDisable()
        {
            //EnvironmentSystemFacade.Inst.RemoveElement(((HeroData)CurCharacterData).ID, this);
        }
    }
}
